import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;

import javax.swing.JPanel;


public class DrawEGnomon extends JPanel implements Printable{
	private int latitude;
	private String timezone;
	private double radius;
	private double gnomonlength;
	private int height;
	private int width;
	private double side = 45;
	
	public DrawEGnomon(int lat, String tz, double rad, double gl){
		this.latitude = lat;
		this.timezone = tz;
		this.radius = rad;
		this.gnomonlength = gl;
		this.width =  (int) (this.radius/0.2321);
		this.height = (int) (this.side/0.09);		
		this.setBackground(Color.white);
		setPreferredSize(new Dimension(this.width, this.height));
	}

	/*
	 * (non-Javadoc)
	 * @see javax.swing.JComponent#paint(java.awt.Graphics)
	 */
	public void paint(Graphics canvas){
		super.paintComponent(canvas);
		int midx = this.width/2;
		int midy = this.height/2;
		double y = this.width*0.1;
		double latRadian = this.latitude * Math.PI/180;
		
		canvas.setFont(new Font("Arial", Font.BOLD, 15));
		canvas.drawString(this.timezone, midx, (int) (this.height*0.2));
		//draw the folding line
		canvas.setColor(Color.blue);
		canvas.drawLine((int) (this.width*0.15), midy, (int) (this.width*0.85), midy);
		canvas.setColor(Color.black);
		//draw the angle
		canvas.drawLine((int) (this.width*0.85), midy, (int) (this.width*0.85 - (y/2)/Math.tan(latRadian)), (int)(midy - y/2));
		canvas.drawLine((int) (this.width*0.85), midy, (int) (this.width*0.85 - (y/2)/Math.tan(latRadian)), (int)(midy + y/2));
		//draw the body of gnomon
		canvas.drawLine((int) (this.width*0.15), (int)(midy - y/2), (int) (this.width*0.85 - (y/2)/Math.tan(latRadian)), (int)(midy - y/2));
		canvas.drawLine((int) (this.width*0.15), (int)(midy + y/2), (int) (this.width*0.85 - (y/2)/Math.tan(latRadian)), (int)(midy + y/2));
		canvas.drawLine((int) (this.width*0.15), (int)(midy - y/2), (int) (this.width*0.15), (int)(midy + y/2));
		canvas.setFont(new Font("Arial", Font.PLAIN, (int) (this.width*0.05)));
		canvas.drawString("N", (int) (this.width*0.15), (int) (midy*0.99));
		//mark the gnomon
		canvas.drawLine((int) ((this.width*0.85 - (y/2)/Math.tan(latRadian))-this.gnomonlength), (int)(midy - y/2),
				(int) ((this.width*0.85 - (y/2)/Math.tan(latRadian))-this.gnomonlength), (int)(midy - y/2 + y*0.25));
		canvas.drawLine((int) ((this.width*0.85 - (y/2)/Math.tan(latRadian))-this.gnomonlength), (int)(midy + y/2),
				(int) ((this.width*0.85 - (y/2)/Math.tan(latRadian))-this.gnomonlength), (int)(midy + y/2 - y*0.25));
	}
	@Override
	public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
		// number of page is zero based
        if (pageIndex > 0) {
            return NO_SUCH_PAGE;
        }

        // User (0,0) is typically outside the
        // imageable area, so we must translate
        // by the X and Y values in the PageFormat
        // to avoid clipping.
        Graphics2D g2d = (Graphics2D) graphics;
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

        // Now we perform our rendering
        //This prints a grey page with the applet name at the top, but no lines.
        //why no lines?
        this.paint(g2d);

        // tell the caller that this page is part
        // of the printed document
        return PAGE_EXISTS;
	}
	
}
